/*
 * Copyright (C) 2008, Velocityme Partnership
 * SprintValueActionForm.java
 *
 * Created on 28 August 2008, 20:12
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.Person;
import com.velocityme.entity.Sprint;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.PersonLocal;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class SprintActionForm extends NodeActionForm {
    
    private int m_startYear;
    private int m_startMonth;
    private int m_startDay;
    private int m_stopYear;
    private int m_stopMonth;
    private int m_stopDay;
    private long m_scrumMasterId;
    private Long[] m_allTeamPersonIds;
    private Long[] m_teamPersonIds;
    
    public void extractSprint(KeyLocal key, Sprint sprint) {
        extractNode(sprint);
        PersonLocal personLocal = lookupPersonBean();
        sprint.setScrumMaster(personLocal.findPerson(key, m_scrumMasterId));
        Calendar startDate = Calendar.getInstance(); startDate.clear();
        startDate.set(m_startYear, m_startMonth, m_startDay);
        sprint.setStartDate(startDate.getTime());
        Calendar stopDate = Calendar.getInstance(); stopDate.clear();
        stopDate.set(m_stopYear, m_stopMonth, m_stopDay, 23, 59, 59);
        sprint.setStopDate(stopDate.getTime());
        sprint.clearTeamMembers();
        if (m_teamPersonIds != null) {
            for (Long id: m_teamPersonIds) {
                Person person = personLocal.findPerson(key, id);
                sprint.addTeamMember(person);
            }
        }
    }

    public int getStartYear() { return m_startYear; }
    
    public void setStartYear(int year) { m_startYear = year; }
    
    public int getStartMonth() { return m_startMonth; }
    
    public void setStartMonth(int month) { m_startMonth = month; }
    
    public int getStartDay() { return m_startDay; }
    
    public void setStartDay(int day) { m_startDay = day; }
    
    public int getStopYear() { return m_stopYear; }
    
    public void setStopYear(int year) { m_stopYear = year; }
    
    public int getStopMonth() { return m_stopMonth; }
    
    public void setStopMonth(int month) { m_stopMonth = month; }
    
    public int getStopDay() { return m_stopDay; }
    
    public void setStopDay(int day) { m_stopDay = day; }

    public void setStartDate(Date start) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        setStartYear(cal.get(Calendar.YEAR));
        setStartMonth(cal.get(Calendar.MONTH));
        setStartDay(cal.get(Calendar.DATE));
    }
    
    public void setStopDate(Date stop) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(stop);
        setStopYear(cal.get(Calendar.YEAR));
        setStopMonth(cal.get(Calendar.MONTH));
        setStopDay(cal.get(Calendar.DATE));
    }
    public Long getScrumMasterId() {
        return m_scrumMasterId;
    }
    
    public void setScrumMasterId(Long scrumMasterId) {
        m_scrumMasterId = scrumMasterId;
    }
    
    public Long[] getAllTeamPersonIds() {
        return m_allTeamPersonIds;
    }
    
    public void setAllTeamPersonIds(Long[] allTeamPersonIds) {
        m_allTeamPersonIds = allTeamPersonIds;
    }
    
    public Long[] getTeamPersonIds() {
        return m_teamPersonIds;
    }
    
    public void setTeamPersonIds(Long[] teamPersonIds) {
        m_teamPersonIds = teamPersonIds;
    }

    private PersonLocal lookupPersonBean() {
        try {
            Context c = new InitialContext();
            return (PersonLocal) c.lookup("java:comp/env/PersonBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
